package blazingbubble.network.game.strategy;

import blazingbubble.game.Bubble;
import blazingbubble.game.Field;
import blazingbubble.game.Game;
import moltencore.network.protocol.BubbleData;
import moltencore.network.protocol.RowPushed;
import org.apache.log4j.Logger;
import spark.network.protocol.Response;
import spark.network.strategy.IProtocolHandleStrategy;
import spark.network.protocol.Acknowledgement;
import spark.network.protocol.Base;
import spark.utility.OutWrapper;

import java.util.Collection;

public class RowPushedStrategy implements IProtocolHandleStrategy<RowPushed> {
	private Logger logger = Logger.getLogger(RowPushedStrategy.class);

	private Game game;

	public RowPushedStrategy(Game game) {
		this.game = game;
	}

	@Override
	public void handle(RowPushed packet, OutWrapper<Response> acknowledgement, Collection<Base> additionalData) {
		Field field = game.getField(packet.getFieldId());
		field.pushCeiling(packet.getDirection());
		for(BubbleData bubbleData : packet.getBubblesData()) {
			field.spawnBubble(makeBubble(bubbleData));
		}
		acknowledgement.value = new Acknowledgement(packet);
	}

	protected Bubble makeBubble(BubbleData bubbleData) {
		Bubble bubble = new Bubble(bubbleData.getType(), bubbleData.getBubbleId());
		bubble.setState(Bubble.State.Fixed);
		bubble.getPosition().copy(bubbleData.getPosition());
		bubble.getVelocity().copy(bubbleData.getVelocity());
		return bubble;
	}
}